package com.tyk.bookstore.front.payment.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyk.bookstore.front.order.model.dto.Order4PaymentRecordDto;
import com.tyk.bookstore.front.order.service.OrderApiService;
import com.tyk.bookstore.front.payment.model.entity.AmountLogDo;
import com.tyk.bookstore.front.payment.model.query.AmountLogPageQuery;
import com.tyk.bookstore.front.payment.model.vo.AmountLogPageVo;
import com.tyk.bookstore.front.payment.model.vo.AmountLogVo;
import com.tyk.bookstore.front.payment.repository.AmountLogRepository;
import com.tyk.bookstore.front.payment.service.AmountLogService;
import com.tyk.bookstore.front.payment.util.MapStructPayment;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author tyk
 * @description 系统余额变更日志
 */
@Service
@RequiredArgsConstructor

public class AmountLogServiceImpl implements AmountLogService {

    private final AmountLogRepository amountLogRepository;
    private final OrderApiService orderApiService;
    private final MapStructPayment mapStructPayment;

    @Override
    public AmountLogVo getById(Long id) {
        AmountLogDo amountLog = amountLogRepository.selectById(id);
        return mapStructPayment.toAmountLogVo(amountLog);
    }

    @Override
    public AmountLogPageVo getPage(AmountLogPageQuery query) {
        // 分页查询
        Page<AmountLogDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<AmountLogDo> wrapper = buildPageQueryWrapper(query);
        amountLogRepository.selectPage(page, wrapper);
        AmountLogPageVo pv = mapStructPayment.toAmountLogPageVo(page);

        // 查询订单编号
        Set<Long> oidSet = page.getRecords().stream().map(AmountLogDo::getSourceIdentification).collect(Collectors.toSet());
        Map<Long, String> oMap = orderApiService
                .getOrderNoListByIdSet(oidSet)
                .stream().collect(Collectors.toMap(
                        Order4PaymentRecordDto::getId,
                        Order4PaymentRecordDto::getOrderNo
                ));

        // 转换
        List<AmountLogPageVo.AmountLog> records = page.getRecords().stream().map(it -> {
            AmountLogPageVo.AmountLog vo = mapStructPayment.toAmountLogPageVo$AmountLog(it);
            vo.setSourceIdentification(oMap.get(it.getSourceIdentification()));
            return vo;
        }).toList();

        // 返回
        pv.setRecords(records);
        return pv;
    }

    /**
     * 构建分类查询Wrapper
     */
    private LambdaQueryWrapper<AmountLogDo> buildPageQueryWrapper(AmountLogPageQuery query) {

        long memberId = StpUtil.getLoginIdAsLong();

        LambdaQueryWrapper<AmountLogDo> w = new LambdaQueryWrapper<>();

        w.eq(AmountLogDo::getMemberId, memberId);
        w.eq(query.getNature() != null, AmountLogDo::getNature, query.getNature());
        w.orderBy(true, false, AmountLogDo::getCreateTime);

        return w;
    }


}

